const {execSql} = require("../utils/db")
class user{
    //获取用户信息
    static async getUser(username){
        let user = await execSql(`select id,username,email,mobile,password from lv_user where username='${username}' or mobile='${username}' or email="${username}"`)
        return user
    }

    //添加用户信息 
    static async addUser(obj){
        let res = await execSql(`INSERT INTO lv_user VALUES (null,'${obj.username}', '${obj.password}', '${obj.nickname}', '${obj.headpic}', '${obj.mobile}', '${obj.email}', '${new Date().getTime()}', '${obj.ip}', '${new Date().getTime()}','${obj.openid}')`)
        return res
    }

    //根据openid 获取用户信息
    static async getUserOpenId(openid){
        let user = await execSql(`select * from lv_user where openid='${openid}'`)
        return user
    }

    //获取用户信息的方法
    static async userInfo(id){
        let user = await execSql(`select * from lv_user where id='${id}'`)
        console.log(`select * from lv_user where id='${id}'`)
        if(user.length==0){
            return false
        }
        let collect = await execSql(`select count(*) num from lv_user_like where uid=${id}`)
        console.log(`select count(*) num from lv_user_like where uid=${id}`)
        user[0].likeNum = collect[0].num
        return user 
    }

    static async saveUser(key,value,id){
        if(key==='mobile'){
            let mobiles = await execSql(`select * from lv_user where id=${id}`)
            console.log(mobiles[0].mobile)
            let len = await execSql(`select * from lv_user where mobile='${value}'`)
            if(mobiles[0].mobile!="undefined" || mobiles[0].mobile.length==11 || len.length>0){
                return false
            }
        }else if(key==='email'){
            let mobiles = await execSql(`select * from lv_user where id=${id}`)
            let len = await execSql(`select * from lv_user where email='${value}'`)
            console.log(mobiles[0].email)
            if(mobiles[0].email!="undefined" || mobiles[0].email.indexOf("@")!=-1 || len.length>0){
                return false   
            }
        }else if(key==='username'){
            let mobiles = await execSql(`select * from lv_user where id=${id}`)
            let len = await execSql(`select * from lv_user where username='${value}'`)
            // console.log(mobiles[0].email)
            if(mobiles[0].username!="undefined" || len.length>0){
                return false
            }
        }else if(key=='openid'){
            let mobiles = await execSql(`select * from lv_user where id=${id}`)
            let len = await execSql(`select * from lv_user where openid='${value}'`)
            // console.log(mobiles[0].email)
            if(len.length>0 || mobiles[0].openid!="undefined"){
                return false
            }
        }
        
        let res = await execSql(`update lv_user set ${key}="${value}" where id=${id}`)
        return res

    }

    static async getKfUser(username){
        let res = await execSql(`select * from lv_kf_user where username="${username}"`)
        return res
    }
}

module.exports =user